﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Layer.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.GUI.
//
//   It is released under the Microsoft Public License (Ms-PL).
//
//   You should have received a copy of the Microsoft Public License
//   along with starLiGHT.GUI. If not, see http://sle.codeplex.com/license.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.GUI are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.GUI
{
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion

    public class Layer : SkinElement, IEquatable<Layer>
    {
        #region Private Members
        private State state;
        private Layer parentLayer;
        private Image image;
        private Margin offset;
        private Margin resize;
        private Margin content;
        private Margin tile;
        private Margin click;
        private int width = -1;
        private int height = -1;
        private Alignment alignment = Alignment.TopLeft;
        private List<Layer> sublayers = new List<Layer>();
        private List<TextLabel> textLabels = new List<TextLabel>();
        #endregion

        public Layer(State state, string name)
            : base(name)
        {
            this.state = state;
        }

        public Layer(Layer parentLayer, string name)
            : base(name)
        {
            this.parentLayer = parentLayer;
        }

        public bool IsRootLayer
        {
            get
            {
                return this.state != null && this.parentLayer == null;
            }
        }

        public Image Image
        {
            get
            {
                return this.image;
            }

            set
            {
                this.image = value;
            }
        }

        public Margin OffsetMargin
        {
            get
            {
                if (this.offset == null)
                {
                    this.offset = new Margin("Offset");
                }

                return this.offset;
            }

            set
            {
                this.offset = value;
            }
        }

        public Margin ResizeMargin
        {
            get
            {
                if (this.resize == null)
                {
                    this.resize = new Margin("Resize");
                }

                return this.resize;
            }

            set
            {
                this.resize = value;
            }
        }

        public Margin ContentMargin
        {
            get
            {
                if (this.content == null)
                {
                    this.content = new Margin("Content");
                }

                return this.content;
            }

            set
            {
                this.content = value;
            }
        }

        public Margin TileMargin
        {
            get
            {
                if (this.tile == null)
                {
                    this.tile = new Margin("Tile");
                }

                return this.tile;
            }

            set
            {
                this.tile = value;
            }
        }

        public Margin ClickMargin
        {
            get
            {
                if (this.click == null)
                {
                    this.click = new Margin("Click");
                }

                return this.click;
            }

            set
            {
                this.click = value;
            }
        }

        public int Width
        {
            get
            {
                return this.width;
            }

            set
            {
                this.width = value;
            }
        }

        public int Height
        {
            get
            {
                return this.height;
            }

            set
            {
                this.height = value;
            }
        }

        public Alignment Alignment
        {
            get
            {
                return this.alignment;
            }

            set
            {
                this.alignment = value;
            }
        }

        public State State
        {
            get
            {
                return this.state;
            }
        }

        public Layer ParentLayer
        {
            get
            {
                return this.parentLayer;
            }
        }

        public Layout Layout
        {
            get
            {
                if (this.state != null)
                {
                    return this.state.Layout;
                }
                else
                {
                    return this.ParentLayer.Layout;
                }
            }
        }

        public IEnumerable<Layer> Layers
        {
            get
            {
                foreach (Layer layer in this.sublayers)
                {
                    yield return layer;
                }
            }
        }

        public IEnumerable<TextLabel> TextLabels
        {
            get
            {
                foreach (TextLabel te in this.textLabels)
                {
                    yield return te;
                }
            }
        }

        public void AddLayer(Layer layer)
        {
            if (layer == null)
            {
                throw new ArgumentNullException("layer");
            }

            this.sublayers.Add(layer);
        }

        public void AddTextLabel(TextLabel label)
        {
            if (label == null)
            {
                throw new ArgumentNullException("label");
            }

            this.textLabels.Add(label);
        }

        #region IEquatable<Layer> Member

        public bool Equals(Layer other)
        {
            return this.State.Equals(other.State) && this.Name.Equals(other.Name);
        }

        #endregion
    }
}
